/**
 * 
 */
package com.cs.common.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;

import com.cpi.user.view.UserView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;


/**
 * Security Filter
 * @author jdelgado
 */

public class AppFilter implements Filter {
	/** Nombre de variable de session para el usuario */
	private static final String USER_SESSION_NAME = "user";
	/** Acci�n de logout */
	private static final String LOGOUT_ACTION = "/logout.do";
	/** Acci�n de login */
	private static final String MAIN_ACTION = "/main.do";
	/** Acci�n de login */
	private static final String LOGIN_ACTION = "/loginUser.do";
	/** Language key first part */
	private static final String LANGUAGE_KEY = "LANGUAGE_CODE_";//$NON-NLS-1$
	/** Language key default last part */
	private static final String LANGUAGE_DEFAULT = "DEFAULT";//$NON-NLS-1$
	/** Error message */
	private static final String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Security actions exception */
	private Collection securityActionsException;
	/** Init filter config */
	private FilterConfig filterConfig = null;
	/**
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		filterConfig = null;
	}
	/** 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = null;
		HttpServletResponse httpServletResponse = null;
		HttpSession httpSession =  null;
		String requestUri = null;
		UserView userView = null;
		String contextPath = null;
		String errorTmp = null;
		
		httpServletRequest = (HttpServletRequest)servletRequest;
		httpServletResponse = (HttpServletResponse)servletResponse;
		contextPath = httpServletRequest.getContextPath();
		requestUri= httpServletRequest.getServletPath();
		httpSession =  httpServletRequest.getSession();
		userView = (UserView)httpSession.getAttribute(USER_SESSION_NAME);
		if( !securityActionsException.contains(requestUri) ){
			if(  userView == null ){
				httpServletResponse.sendRedirect(new StringBuffer(contextPath).append(LOGOUT_ACTION).toString());
			}else{
				filterChain.doFilter(servletRequest, servletResponse);
			}
		}else{
			if( requestUri.equals(MAIN_ACTION) ){
				errorTmp = (String)httpSession.getAttribute(ERROR_MESSAGE);
				httpSession.invalidate();
				httpSession = httpServletRequest.getSession();
				httpSession.setAttribute(ERROR_MESSAGE,errorTmp);
			}
			filterChain.doFilter(servletRequest, servletResponse);
		}
	}
	/**
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig nFilterConfig) throws ServletException {
		filterConfig = nFilterConfig;
		securityActionsException = new ArrayList(0);
		securityActionsException.add(LOGIN_ACTION);
		securityActionsException.add(LOGOUT_ACTION);
		securityActionsException.add(MAIN_ACTION);
		System.out.println(filterConfig);
	}
	/**
	 * Sets locale for user
	 * @param request Request neeeds for super.setLocale
	 * @param languageId Languaje id
	 */
	protected void setLocale(HttpServletRequest httpServletRequest, HttpSession httpSession, String languageId){
		String languageKey = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		
		systemParameterCollection = SystemParameterCollection.getInstance();
		languageKey = new StringBuffer(LANGUAGE_KEY).append(languageId).toString();
		systemParameterBean = (SystemParameterBean)systemParameterCollection.get(languageKey);
		if( systemParameterBean == null ){
			languageKey = new StringBuffer(LANGUAGE_KEY).append(LANGUAGE_DEFAULT).toString();
			systemParameterBean = (SystemParameterBean)systemParameterCollection.get(languageKey);
		}
		httpSession.setAttribute(Globals.LOCALE_KEY, new java.util.Locale(systemParameterBean.getValue()) );
		httpServletRequest.getSession(false).setAttribute("LANGUAGE_SESSION", systemParameterBean.getValue());
	}
}